package cn.wolfcode.crm.service.impl;/**
 * Created by 10797 on 2018/7/14.
 */

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPaymentResultService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-14 14:25
 **/
@Service
public class PaymentResultServiceImpl implements IPaymentResultService{

    @Autowired
    private PaymentResultMapper paymentResultMapper;
    @Autowired
    private PaymentBillMapper paymentBillMapper;
    @Autowired
    private GuaranteeBillMapper guaranteeBillMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SalesReporMapper salesReporMapper;
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public void save(PaymentResult paymentResult) {
        paymentResultMapper.insert(paymentResult);
    }

    @Override
    public void delete(Long id) {
        paymentResultMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(PaymentResult paymentResult) {
        paymentResultMapper.updateByPrimaryKey(paymentResult);
    }

    @Override
    public PaymentResult get(Long id) {
        return paymentResultMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<PaymentResult> getAll() {
        return paymentResultMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int count = paymentResultMapper.queryForCount(qo);
        if(count == 0){
            return new PageResult();
        }
        List<PaymentResult> list = paymentResultMapper.queryForList(qo);
        return new PageResult(count,list);
    }

    @Override
    public void add(Long id, Long payWayId, Date beginDate) {
        //1.创建缴费单据对象
        PaymentResult paymentResult = new PaymentResult();
        //2.获取付款对象
        PaymentBill paymentBill = paymentBillMapper.selectByPrimaryKey(id);
        //4.1设置投保人信息(来自原已审核未缴费的单据当中的正式客户)
        paymentResult.setPolicyClient(paymentBill.getPolicyClient());
        //4.2设置核保的单号
        paymentResult.setNumber(paymentBill.getNumber());
        //4.3设置保单金额
        paymentResult.setTotalPrice(paymentBill.getPrice());
        //4.4设置年限
        paymentResult.setLimitYear(paymentBill.getLimitYear());
        //4.5设置缴费方式
        paymentResult.setPayWayId(payWayId);
        //4.6设置起止日期
        //4.6.1设置开始时间就是当前传递进来的时间
        paymentResult.setBeginDate(beginDate);
        //4.6.2设置结束时间应该是当前时间加上购买的年限
        Calendar calendar=Calendar.getInstance();
        //4.6.2.1设置当前时间为开始时间
        calendar.setTime(beginDate);
        //4.6.2.2设置当前时间添加上保险年限
        calendar.add(Calendar.YEAR,paymentBill.getLimitYear());
        //4.6.2.3获取结束时间为上面的结果
        Date endDate =calendar.getTime();
        //4.6.2.4设置结束时间
        paymentResult.setEndDate(endDate);
        //4.7设置当前保单的保险公司
        Company company = companyMapper.selectByPrimaryKey(paymentBill.getCompany().getId());
        paymentResult.setCompany(company);
        //4.8设置当前缴费的操作人
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        paymentResult.setOpEmployee(currentUser);
        //5.保存缴费单据结果
        paymentResultMapper.insert(paymentResult);


        //6.改变保单的状态
        //6.1通过保单编码查询当前保单
        GuaranteeBill guaranteeBill = guaranteeBillMapper.getBillBySn(paymentBill.getNumber());
        //6.2设置起保日期
        guaranteeBill.setBeginDate(beginDate);
        //6.3设置止保日期
        guaranteeBill.setEndDate(endDate);
        //6.4修改保单状态为已缴费状态
        guaranteeBill.setState(GuaranteeBill.ALREADYPAY);
        //6.5设置当前状态到保单历史记录当中去
        guaranteeBillMapper.updateHistoryBillState(guaranteeBill);
        //6.6设置缴费单据状态为已缴费
        paymentBillMapper.changeState(paymentBill);

        //7.缴费的同时保存销售单据
        SalesRepor salesRepor = new SalesRepor();
        //7.1设置销售业绩的时间
        salesRepor.setVdate(new Date());
        //7.2设置销售总金额
        salesRepor.setAmount(paymentBill.getPrice());
        //7.3设置当前销售人员
        salesRepor.setSalemanId(currentUser.getId());
        //7.3设置当前保险机构的id
        salesRepor.setCompanyId(company.getId());
        //7.4设置当前保险产品id
        //7.4.1通过当前保单编号从中间表当中查询出当前的id
        Product p=productMapper.selectPIdByBillNumber(Long.valueOf(paymentBill.getNumber()));
        salesRepor.setProductId(p.getId());
        salesReporMapper.insert(salesRepor);

        //8.改变当前客户状态为正式客户
        Client policyClient = guaranteeBill.getPolicyClient();
        clientMapper.changeStateToVIP(policyClient);

    }
}
